# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from datetime import datetime,timedelta

class RecentManager(models.Manager):
    def get_query_set(self):
        '''
        Returns objects from last 5 days
        '''
        date_now = datetime.now()
        date_days = timedelta(days=5)
        manager = super(RecentManager, self)
        query = manager.get_query_set()
        return query.filter(date__gt=date_now-date_days,active=True).order_by('-date')
    
class News(models.Model):
    '''
    News are grouped by days, so for each day you group the news and display them
    If you want a comment a news, you don't bind the comment to the news but to the day
    '''
    author = models.ForeignKey(User)
    content = models.TextField("Content")
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')))
    created_date = models.DateField("Date of creation",auto_now_add=1)
    date = models.DateField("Updated date",auto_now_add=1)
    recent  = RecentManager()
    objects = models.Manager()
    active = models.BooleanField(default=True)
    
    def __str__(self):
        return self.content
    class Admin:
        pass
    
class Package(models.Model):
    '''
    Packages are retrieved via cheeseshop RSS and stored into database
    for archive and easier displaying on the site
    '''
    author = models.CharField(maxlength=256)
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')))
    created_date = models.DateField("Date of content",auto_now_add=1)
    recent  = RecentManager()
    objects = models.Manager()
    title = models.CharField("Title",maxlength=128)
    url = models.CharField(maxlength=512)
    content = models.TextField("Content")
    date = models.DateField("Date of package")
    active = models.BooleanField(default=True)
    
    def __str__(self):
        return self.title

    class Admin:
        pass

class Post(models.Model):
    '''
    Blog posts from other blogs that will be selected in subscriptions
    '''
        
    author = models.CharField(maxlength=256)
    content = models.TextField("Content")
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')))
    created_date = models.DateField("Date of content",auto_now_add=1)
    date = models.DateField("Date of post")
    recent  = RecentManager()
    objects = models.Manager()
    title = models.CharField("Title",maxlength=128)
    url = models.CharField(maxlength=512)
    active = models.BooleanField(default=True)
    
    
    def __str__(self):
        return self.title
    
    class Admin:
        pass
   
   
class Subscription(models.Model):
    '''
    A subscription to a news/blog source
    '''
    title = models.CharField("Source title",maxlength=128)
    url = models.CharField("URL",maxlength=512)
    author = models.ForeignKey(User)
    active = models.BooleanField(default=True)
    
    class Admin: pass

    def __str__(self):
        return self.url
    
class Comment(models.Model):
    '''
    because there is no object to which comment can be bound to
    we bound it to a specific date that points to specific news
    '''
    commented_day = models.DateField("Commented day")
    
    author = models.ForeignKey(User)
    content = models.TextField("Content")
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')))
    created_date = models.DateField("Date of content",auto_now_add=1)
    recent  = RecentManager()
    objects = models.Manager()
    title = models.CharField("Title",maxlength=128)
    
    def __str__(self):
        return self.title
    
    class Admin:
        pass
 
class UserProfile(models.Model):
    '''
    extended django's User model used for registration, stores
    activation key and it's expiration date
    '''
    #user = models.OneToOneField(User)
    user = models.ForeignKey(User, unique=True)
    activation_key = models.CharField(maxlength=40)
    key_expires = models.DateTimeField()
    public_email = models.BooleanField(default=False)
    send_notices = models.BooleanField(default=False)
    company = models.CharField(maxlength=128)
    status = models.CharField(maxlength=30, choices=(('dijak', 'dijak'), ('student', 'študent'), ('zaposlen', 'redno zaposlen'), ('upokojenec', 'upokojenec'), ('ostalo', 'ostalo')))
    signature = models.CharField(maxlength=255)
    
    def __str__(self):
        return self.user.username

    class Admin:
        pass